package com.hzit.sk.service.impl;
import com.hzit.sk.constant.RedisKeyConstant;
import com.hzit.sk.mapper.UserInfoMapper;
import com.hzit.sk.model.UserInfo;
import com.hzit.sk.req.UserLoginReq;
import com.hzit.sk.resp.Response;
import com.hzit.sk.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * 用户相关的业务接口具体实现类
 */
@Service
public class UserServiceImpl implements IUserService {

    Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserInfoMapper userInfoMapper;

   @Autowired
    private ValueOperations valueOperations;

   @Autowired
    private HashOperations hashOperations;


    @Override
    public Response userLogin(UserLoginReq userLoginReq) {

        Response response = new Response();

        //shiro 去做登录
       UserInfo userInfo = userInfoMapper.queryInfoByMobile(userLoginReq.getMobile());

       if(ObjectUtils.isEmpty(userInfo)){
           return response.getFailData("手机号不存在");
       }

       //不为空的字符串一定要写签名

       //TODO

       if(!"ae2fe40a6242ef07a35a30da2232e10a".equals(userInfo.getPassword())){
           return response.getFailData("密码不正确");
       }

        //登录成功，吧用户信息写入到redi
//        valueOperations.set(RedisKeyConstant.USER_LOGIN + userInfo.getId() ,userInfo,2, TimeUnit.HOURS);

        hashOperations.put(RedisKeyConstant.USER_LOGIN + userInfo.getId(),"userInfo",userInfo);

        response = response.getSuccessData("登录成功");

        HashMap<String,String> hashMap = new HashMap<String,String>();

        hashMap.put("userId",userInfo.getId().toString());

        response.setData(hashMap);

        return response;
    }


}
